﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Theoreme.Samples.UnitTesting.Mocks.BLL
{
    /// <summary>
    /// This class represent all of the business logic in our application. 
    /// It would interact with the Data Access tier and return information to the UI layer for display.
    /// 
    /// We wire together our objects using Constructor based Dependency Injection. 
    /// 
    /// All of the dependent Objects are sent in through the constructor. 
    /// 
    /// This allows for the loose coupling since the PersonService doesn’t know about the Implementing class, 
    /// but only the interface. 
    /// 
    /// Since it’s done in the constructor we can also never have an invalid PersonService as would be the 
    /// case if there was a setter for the IPersonRepository implementation.
    /// </summary>
    public class PersonService
    {
        private IPersonRepository personRepos;


        public PersonService(IPersonRepository repos)
        {
            personRepos = repos;
        }
        
        public List<Person> GetAllPeople()
        {
            return personRepos.GetPeople();
        }
        
        
        public List<Person> GetAllPeopleSorted()
        {
            List<Person> people = personRepos.GetPeople();

            people.Sort(delegate(Person lhp, Person rhp)
            {
                return lhp.LastName.CompareTo(rhp.LastName);
            });
            return people;
        }
        
        
        public Person GetPerson(string id)
        {
            try
            {
                return personRepos.GetPersonById(id);
            }
            catch (ArgumentException)
            {
                return null; // no person with that id was found
            }
        }
    }
}


